package com.reader;

import java.io.File;
import java.io.FileInputStream;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;

import com.bean.RowBean;

public class Reader {

	public Reader() throws Exception {
		String fileName = "TT-2014-08-01 al 2014-08-05 - Fernando Tapia.xls";
		readXLS(fileName);
		// readXLSX(fileName);
	}

	private void readXLS(String fileName) throws Exception {
		FileInputStream file = new FileInputStream(new File(fileName));
		HSSFWorkbook workbook = new HSSFWorkbook(file);
		HSSFSheet sheet = workbook.getSheet("TimeTracking");
		List<RowBean> rowList = new LinkedList<RowBean>();
		for (Row row : sheet) {
			if (row.getRowNum() == 0) {
				continue;
			}
			RowBean rowBean = new RowBean();
			for (int column = 0; column < 11; column++) {
				Cell cell = row.getCell(column);
				switch (column) {
				case 0: {
					rowBean.setDeveloper(getCleanString(cell));
					break;
				}
				case 1: {
					rowBean.setNameProject(getCleanString(cell));
					break;
				}
				case 2: {
					rowBean.setStartTime(getAsDate(cell));
					break;
				}
				case 3: {
					rowBean.setEndTime(getAsDate(cell));
					break;
				}
				case 4: {
					rowBean.setDate(getAsDate(cell));
					break;
				}
				case 5: {
					rowBean.setTotalHours(getAsDate(cell));
					break;
				}
				case 6: {
					rowBean.setNameRequirement(getCleanString(cell));
					break;
				}
				case 7: {
					rowBean.setApplication(getCleanString(cell));
					break;
				}
				case 8: {
					rowBean.setBusinessAnalyticsAssigned(getCleanString(cell));
					break;
				}
				case 9: {
					rowBean.setNumberRequirement(getCleanString(cell));
					break;
				}
				case 10: {
					rowBean.setType(getCleanString(cell));
					break;
				}
				}
			}
			if(!rowBean.isEmpty()){
				rowList.add(rowBean);
			}
		}
		
		for(RowBean rowBean: rowList){
			System.out.println(rowBean.toString());
		}
	}

//	private void readXLSX(String fileName) throws Exception {
//		FileInputStream file = new FileInputStream(new File(fileName));
//		// Get the workbook instance for XLS file
//		XSSFWorkbook workbook = new XSSFWorkbook(file);
//		// Get first sheet from the workbook
//		XSSFSheet sheet = workbook.getSheetAt(0);
//		// Get iterator to all the rows in current sheet
//		Iterator<Row> rowIterator = sheet.iterator();
//		while (rowIterator.hasNext()) {
//			Row row = rowIterator.next();
//			// Get iterator to all cells of current row
//			Iterator<Cell> cellIterator = row.cellIterator();
//			while (cellIterator.hasNext()) {
//				Cell cell = cellIterator.next();
//				System.out.println(getCleanString(cell));
//			}
//		}
//	}

	private static String getCleanString(Cell cell) {
		String value = null;
		if (cell != null) {
			value = cell.getStringCellValue();
			value = value != null ? value.replaceAll("\\p{C}", "") : null;
			value = value != null ? value.replaceAll("\\p{javaSpaceChar}{2,}"," ") : null;
			value = value != null ? value.trim() : null;
			value = value != null ? (value.equals("") ? null : value) : null;
		}
		return value;
	}

	private static Date getAsDate(Cell cell) {
		Date value = null;
		if (cell != null) {
			value = cell.getDateCellValue();
		}
		return value;
	}

	public static void main(String[] args) throws Exception {
		new Reader();
	}

}
